package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.*;
import com.hxgz.steel.domain.*;
import com.hxgz.steel.dto.BackendUserDto;
import com.hxgz.steel.dto.CompanyCertDto;
import com.hxgz.steel.service.BackendUserService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.hxgz.steel.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BackendUserServiceImp implements BackendUserService {

  @Autowired
  UserMapper userMapper;
  @Autowired
  AddressMapper addressMapper;
  @Autowired
  CompanyCertMapper companyCertMapper;
  @Autowired
  private IntegralMapper integralMapper;
  @Autowired
  private SocerManagerMapper socerManagerMapper;

  @Override
  public ServerResponse checkUser(HashMap<Object, Object> map, Page page) {
    PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
    List<User> list = userMapper.getAuthenticationUser(map);
    PageInfo pageInfo = new PageInfo(list);
    return ServerResponse.createBySuccess(pageInfo);
  }

  @Override
  public ServerResponse checkUserId(String id) {
    User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
    List<Address> addressList = addressMapper.getAddressList(user.getId());
    CompanyCert companyCert = companyCertMapper.queryCertByUserId(user.getId());
    BackendUserDto backendUserDto = new BackendUserDto();
    backendUserDto.setAddressList(addressList);
    backendUserDto.setUserDomain(user);
    backendUserDto.setCompanyCertDomain(companyCert);
    return ServerResponse.createBySuccess(backendUserDto);
  }

  @Override
  public ServerResponse checkCompanyCert(HashMap<Object, Object> map, Page page) {
    PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
    List<CompanyCertDto> list = companyCertMapper.getCompanyList(map);
    PageInfo pageInfo = new PageInfo(list);
    return ServerResponse.createBySuccess(pageInfo);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public ServerResponse checkCompanyCertId(String id, Integer status) throws Exception {
    CompanyCert companyCert = companyCertMapper.selectByPrimaryKey(Long.valueOf(id));
    int isSuccess = 0;
    int userSuccess = 0;
    if (status != null) {
      if (status == 0) {
        companyCert.setStatus(2);
        //添加积分
        Long integralId = Long.valueOf(5);
        SocerManager socerManager = socerManagerMapper.selectByPrimaryKey(integralId);
        if (new Integer(1).equals(socerManager.getStatus())) {
          //查询用户
          User user = userMapper.selectByPrimaryKey(companyCert.getUserId());
          //原有积分
          int oldScore = user.getScore();
          //需要添加的积分
          int addScore = Integer.valueOf(socerManager.getSocer());
          //添加后的积分
          int newScore = oldScore + addScore;
          user.setScore(newScore);
          //更新用户数据
          userSuccess = userMapper.updateByPrimaryKeySelective(user);
          Integral integral = new Integral();
          integral.setUserId(companyCert.getUserId());
          integral.setInSource(integralId);
          integral.setType(0);
          integral.setInChange(addScore);
          //插入积分明细
          integralMapper.insertSelective(integral);
        }
      } else {
        companyCert.setStatus(1);
      }
      if(companyCert.getStatus()==2){
        companyCert.setAuthenticationTime(DateTimeUtil.dateToStr(new Date()));
      }
      //更新审核状态
      int isChange = companyCertMapper.updateByPrimaryKeySelective(companyCert);
    }
    return ServerResponse.createBySuccess(companyCert);
  }

  /**
   * 获取积分变动
   *
   * @param userId     登陆的用户id
   * @param integralId 要变动的积分id
   * @return
   * @throws Exception
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public ServerResponse changeIntegral(Long userId, Long integralId) throws Exception {
    //查询积分类别获取要增加的积分数
    SocerManager socerManager = socerManagerMapper.selectByPrimaryKey(integralId);
    //判断积分管理是开启还是关闭
    //查询登录用户的信息
    User user = userMapper.selectByPrimaryKey(userId);
    if (!new Integer(3).equals(user.getStatus())) {
      if (new Integer(1).equals(socerManager.getStatus())) {
        int addScore = Integer.valueOf(socerManager.getSocer());
        //原有积分
        int oldScore = user.getScore();
        //添加后的积分
        int newScore = oldScore + addScore;
        user.setScore(newScore);
        //更新用户数据
        Integer userSuccess = userMapper.updateByPrimaryKeySelective(user);
        //插入积分明细
        Integral integral = new Integral();
        integral.setUserId(userId);
        integral.setInSource(integralId);
        //增加
        integral.setType(0);
        integral.setInChange(addScore);
        //插入积分明细
        Integer isSuccess = integralMapper.insertSelective(integral);
        if (isSuccess < 1 || userSuccess < 1) {
          throw new Exception();
        } else {
          return ServerResponse.createBySuccessMessage("操作成功");
        }
      }
    }
    return ServerResponse.createBySuccessMessage("操作成功");
  }
}
